//
//  NativeMediaPicker.m
//  NativeMediaPicker
//
//  Created by Hai Trinh on 5/17/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "NativeMediaPicker.h"

@implementation NativeMediaPicker

FREContext g_ctx;
NativeMediaPickerHelper *nativeMediaPickerHelper;

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }
    
    return self;
}

//open media picker

FREObject PKshowMediaPicker(FREContext ctx, void* funcData, uint32_t argc, FREObject argv[]) {
    nativeMediaPickerHelper = [[NativeMediaPickerHelper alloc] init];
    [nativeMediaPickerHelper setContext:ctx];
    [nativeMediaPickerHelper showMediaPicker];
    return NULL;   
}

//Play and Pause controller

FREObject PKplaySong(FREContext ctx, void* funcData, uint32_t argc, FREObject argv[]) {
    nativeMediaPickerHelper = [[NativeMediaPickerHelper alloc] init];
    [nativeMediaPickerHelper setContext:ctx];
    uint32_t commandLength;
    const uint8_t  *commandCString;
    NSString *commandString = nil;
    //Create NSStrings from CStrings
    if (FRE_OK == FREGetObjectAsUTF8(argv[0], &commandLength, &commandCString)) {
        commandString = [NSString stringWithUTF8String:(char*)commandCString];
        [nativeMediaPickerHelper playSong:commandString];
    }
    
    return NULL;   
}

//Seek for slider

FREObject PKseek(FREContext ctx, void* funcData, uint32_t argc, FREObject argv[]) {
    nativeMediaPickerHelper = [[NativeMediaPickerHelper alloc] init];
    [nativeMediaPickerHelper setContext:ctx];
    uint32_t playbackTimeLength;
    const uint8_t  *playbackTimeCString;
    NSString *playbackTimeString = nil;
    //Create NSStrings from CStrings
    if (FRE_OK == FREGetObjectAsUTF8(argv[0], &playbackTimeLength, &playbackTimeCString)) {
        playbackTimeString = [NSString stringWithUTF8String:(char*)playbackTimeCString];
        [nativeMediaPickerHelper seek:[playbackTimeString intValue]];
    }
    
    return NULL;   
}

//------------------------------------
//
// Required Methods.
//
//------------------------------------

// ContextInitializer()
//
// The context initializer is called when the runtime creates the extension context instance.
void ContextInitializer(void* extData, const uint8_t* ctxType, FREContext ctx,
                        uint32_t* numFunctionsToTest, const FRENamedFunction** functionsToSet)
{
    //we expose two methods to ActionScript
    *numFunctionsToTest = 3;
    
	FRENamedFunction* func = (FRENamedFunction*) malloc(sizeof(FRENamedFunction)*3);
	func[0].name = (const uint8_t*) "showMediaPicker";
	func[0].functionData = NULL;
    func[0].function = &PKshowMediaPicker;
    
    func[1].name = (const uint8_t*) "playSong";
	func[1].functionData = NULL;
    func[1].function = &PKplaySong;
    
    func[2].name = (const uint8_t*) "seek";
	func[2].functionData = NULL;
    func[2].function = &PKseek;
    
	*functionsToSet = func;
	
	g_ctx = ctx;
}

// ContextFinalizer()
//
// The context finalizer is called when the extension's ActionScript code
// calls the ExtensionContext instance's dispose() method.
// If the AIR runtime garbage collector disposes of the ExtensionContext instance, the runtime also calls
// ContextFinalizer().

void ContextFinalizer(FREContext ctx) {
    [nativeMediaPickerHelper setContext:NULL];
    [nativeMediaPickerHelper release];
    nativeMediaPickerHelper = nil;
    return;
}

// ExtInitializer()
//
// The extension initializer is called the first time the ActionScript side of the extension
// calls ExtensionContext.createExtensionContext() for any context.
void ExtInitializer(void** extDataToSet, FREContextInitializer* ctxInitializerToSet,
                    FREContextFinalizer* ctxFinalizerToSet) {
    
    *extDataToSet = NULL;
    *ctxInitializerToSet = &ContextInitializer;
    *ctxFinalizerToSet = &ContextFinalizer;
    
}

// ExtFinalizer()
//
// The extension finalizer is called when the runtime unloads the extension. However, it is not always called.
void ExtFinalizer(void* extData) {
    
    return;
}

@end
